home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-12-11 | 8.2 KB | 263 lines | [TEXT/PJMM] |
- { This file shows how you can write a simple application using MacStarter_Pascal. }
- { This file is a modified version of noCommentProcs.p that implements a very simple }
- { "calculator". The modifications are commented. This example shows how to use }
- { some input box and control "windowDecorations", and how to use a window class }
- { other than xWindow itself as a base for your own window class. }
-
- { To see what the example does, replace the applicationProcs.p file in the generic.π }
- { project file and add the file xTextWindow.p to the project BEFORE this file. Then run it. }
-
- unit applicationProcs;
-
- interface
-
- uses
- xWindow, xControlDecoration, xInputDecoration, { }
- xTextWindow; { This example is based on xTextWindow, rather than on }
- { xWindow directly, so I have to specify that the unit xTextWindow }
- { is to be used. }
-
- const
-
- maxSleepTime = 5;
- ApplicationShortName = 'MiniCalc'; { change the application name, long name and comment line. }
- ApplicationLongName = 'A simple calcuator pprogram';
- AuthorName = 'David Eck';
- AuthorAddress1 = 'Hobart and William Smith Colleges';
- AuthorAddress2 = 'Geneva, NY 14456';
- AuthorAddress3 = '(Email Address: eck@hws.BITNET)';
- CommentLine = 'This is a sample program created using the application shell MacStarter_pascal.';
-
-
-
- var
- editMenu: MenuHandle;
- fileMenu: MenuHandle;
-
-
- procedure InitializeApplication;
- procedure CleanUpApplication;
- procedure DoEditMenu (itemNum: integer);
- procedure DoFileMenu (itemNum: integer;
- var done: boolean);
- procedure DoOtherMenu (menuNum, itemNum: integer);
- procedure UpdateMenus;
- procedure ApplicationIdle;
-
-
- implementation
-
-
- type
-
- myWin = object(xTextWindow) { based on xTextWindow, not xWindow }
-
- { A window with two input boxes for numbers, a button to perform a calculation }
- { with those numbers, a radiogroup specifying which of the operations + - * / }
- { will be performed on those numbers, and a large text area that will be used to }
- { display an ongoing "transcript" of the calculations performed. }
-
- data1, data2: xRealInput; { number input boxes }
- operator: xRadioGroup; { radio group for setting operation }
-
- procedure setDefaults;
- override;
- { override to create space at the top of the text in the window, and to set a }
- { larger minimum window size; also, locks the text, so user can't type in text }
- { area. }
-
- procedure openInRect (title: string;
- left, top, right, bottom: integer);
- override;
-
- procedure doRedraw (badRect: Rect);
- override;
-
- {I have no need to override the procedures doContentClick and doKey }
- { for my window class, so I have removed their declarations here, and have removed }
- { their definitions that occured later in the file noCommentsProcs. }
-
- end; { definition of myWin }
-
-
- { If you use a button in your window, you will probably create a new class as }
- { a subclass of xButton or xDefaultButton, and override the HandleClick procedure }
- { in that class to specify what the button does. }
-
- calcButton = object(xDefaultButton)
- { The button type for use in myWin; it will perform the calculation and }
- { append the results to the transcript when it is clicked on (and also, since }
- { it is a default button, when the user presses Return or Enter. }
- procedure HandleClick;
- override;
- end;
-
- procedure calcButton.HandleClick;
- { this procedure is called when the calculation button is presses; it will do the }
- { calculation specified by the current setting of the radio group, and will }
- { display the results in the window below. }
- var
- win: myWin; { the window that contains the button }
- x, y: extended; { number from input boxes }
- xStr, yStr: string; { contents of input boxes as strings }
- ans: extended; { result of calculation }
- ansStr: string; { result of calculation, represented as a string }
- op: char; { specified operation: + - * / }
- err: boolean; { checks if the contents of input boxes are legal }
- begin
- { "itsWindow" is an instance variable for the button object that tells which }
- { xWindow its in; since the button is actually in a myWin, I typecast itsWindow }
- { to type myWin to get access to its instance variables. }
- win := myWin(itsWindow);
- with win do begin
- data1.getNumber(x, err); { get the first number, and check that there was no error }
- if err then
- EXIT(HandleClick);
- data2.getNumber(y, err); { second number }
- if err then
- EXIT(HandleClick);
- case operator.selected of { check which radio button is selected, and do operation }
- 1: begin
- ans := x + y;
- op := '+';
- end;
- 2: begin
- ans := x - y;
- op := '-'
- end;
- 3: begin
- ans := x * y;
- op := '*'
- end;
- 4:
- if y <> 0 then begin
- ans := x / y;
- op := '/'
- end
- else begin
- TellUser('Error: Division by zero is an illegal operation.');
- EXIT(HandleClick);
- end;
- end;
- if ans = 0 then { convert ans to a string for display }
- ansStr := '0'
- else if (abs(ans) < 5e-7) | (abs(ans) > 5e8) then
- ansStr := StringOf(ans : 20)
- else
- ansStr := StringOf(ans : 1 : 10);
- data1.GetContents(xStr); { get contents of input boxes, as strings }
- data2.GetContents(yStr);
- appendString(StringOf(xStr, ' ', op, ' ', yStr, ' = ', ansStr)); { add calculation to transcript. }
- appendCR; { note that appendString and appendCR are procedure in class xTextWindow. }
- appendCR;
- end;
- end;
-
- procedure myWin.setDefaults;
- begin
- inherited setDefaults;
- locked := true; { instance variable in class xTextWindow; user will not be able to edit text. }
- vScrollTopOffset := 82; { leave space at top of vertical scroll bar. }
- topTextOffset := 82; { leave space at top of the text (transcript) in the window }
- SetMinDragWidth(340); { set minumum allowed size during window resizing }
- SetMinDragHeight(150);
- end;
-
- procedure myWin.openInRect (title: string;
- left, top, right, bottom: integer);
- { open a window and add all the decorations }
- var
- bttn: calcButton; { the button that will perform the calculations }
- begin
- inherited openInRect(title, left, top, right, bottom); { open a standard xTextWindow }
- new(bttn); { install calculation button }
- bttn.setUp(self, 'Calculate', 234, 24, 72, 34);
- new(operator); { install radio group with one radio button for each possible operation }
- operator.setUp(self, 'Add\Subtract\Multiply\Divide', 10, 10);
- new(data2); { install a real-number input box }
- data2.setUp(self, 100, 44, 110, 24);
- new(data1); { install other input box }
- data1.SetUp(self, 100, 10, 110, 24);
- end;
-
- procedure myWin.doRedraw (badRect: Rect);
- begin
- inherited doRedraw(badRect); { draws text and decorations (button, radiogroup, and input boxes) }
- PenSize(1, 2); { Draw a line across the screen, sepatatind decorations from text }
- MoveTo(0, 80);
- LineTo(theWindow^.portRect.right, 80);
- PenSize(1, 1);
- end;
-
-
- procedure OpenAWindow;
- { open a window; called by InitializeApplication and in response to New command }
- var
- Win: myWin;
- begin
- new(Win);
- Win.open('Calculation Window, with Transcript');
- end;
-
- {--------------------------------------------------------------------}
-
- procedure InitializeApplication;
- begin
- OpenAWindow;
- end;
-
- procedure CleanUpApplication;
- begin
- end;
-
- procedure UpdateMenus;
- var
- win: WindowPtr;
- xWin: xWindow;
- begin
- win := FrontWindow;
- if Window2xWindow(win, xWin) then
- EnableItem(fileMenu, 2)
- else
- DisableItem(fileMenu, 2);
- end;
-
-
- procedure CloseFrontWindow;
- var
- X: xWindow;
- begin
- if window2xWindow(FrontWindow, X) then
- X.doClose
- end;
-
- procedure DoFileMenu (itemNum: integer;
- var done: boolean);
- begin
- case itemNum of
- 1: { New command }
- OpenAWindow;
- 2: { Close command }
- CloseFrontWindow;
- 4: { Quit command }
- done := true;
- end;
- end;
-
- procedure DoEditMenu (itemNum: integer);
- begin
- end;
-
- procedure DoOtherMenu (menuNum, itemNum: integer);
- begin
- end;
-
- procedure ApplicationIdle;
- var
- X: xWindow;
- begin
- if window2xWindow(FrontWindow, X) then
- X.idle;
- end;
-
- end.